cd c:\manoj\projects\alexei_gic_jul2000\;
s = dir('*.txt')

for i = 1:length(s),
    filename(i,:) = s(i).name;
end;

AA= filename(:,12:14);
BB=unique(AA,'rows');
counter_i = 1;

counter_i = 1;
for i = 1:31,
    for j = 0:23, 
        for k = 0:59,
        global_fday(counter_i) = datenum(2000,7,i,j,k,0);
        counter_i = counter_i+1;
        end;
    end;
end;
gfday = global_fday-datenum(2000,1,1);
counter_i = 1;

DataXYZ = ones([length(BB),3,length(gfday)]);
DataHD = ones([length(BB),2,length(gfday)]);

for i = 1:length(s),
[Element,Table,Description, Units, Sampling, Missing, times, dates, values, qualifier,description,Name,fday]= readFiles(s(i).name);    
L = values == str2num(Missing);
values(L) = NaN;

fprintf('%s %s\n', Element, Name); 
if Element == 'X' | Element == 'Y' | Element == 'Z',
    station_code = AA(i,:);
    [ia,ib,c] = intersect(BB,station_code,'rows');
    [ia1,ib1,c1] = intersect(['X','Y','Z'],Element);
    [ia2,ib2,c2] = intersect(gfday,fday);
    if ~isempty(ia) & ~isempty(ia1) & ~isempty(ib2),
    DataXYZ(ib,ib1,ib2) = values(c2);
    end;
    
end;

if Element == 'H' | Element == 'D',
    
    station_code = AA(i,:);
    [ia,ib,c] = intersect(BB,station_code,'rows');
    [ia1,ib1,c1] = intersect(['H','D'],Element);
    [ia2,ib2,c2] = intersect(gfday,fday);
    if ~isempty(ib) & ~isempty(ib1) & ~isempty(ib2),
    DataHD(ib,ib1,ib2) = values(c2);
    end;
    stationHD(counter_i,:) = station_code;
    counter_i = counter_i+1;
end;
end;
%save c:\manoj\projects\alexei_gic_jul2000\July2000minutedata_March232009
load  c:\manoj\projects\alexei_gic_jul2000\July2000minutedata_March232009 DataHD DataXYZ BB stationHD fday;
[ia,ib,c] = intersect(BB,'DIK','rows');
DataHD(ib,2,:) = DataHD(ib,2,:)/10;%mistake in spidr entry
[ia,ib,c] = intersect(BB,'CCS','rows');
DataHD(ib,2,:) = DataHD(ib,2,:)/10;
DataXY(:,1,:) = DataHD(:,1,:).*cos(DataHD(:,2,:)./60.*(pi/180)); %X
DataXY(:,2,:) = DataHD(:,1,:).*sin(DataHD(:,2,:)./60.*(pi/180)); %Y


CC = unique(stationHD,'rows');
[ia,ib,c] = intersect(BB,CC,'rows');


DataXYZ(ib,1,:) = DataXY(ib,1,:);
DataXYZ(ib,2,:) = DataXY(ib,2,:);

[a,b] = xlsread('C:\Manoj\geomag\figures\Gmagobs.xls',-1);
%-1 allows interactive data selection - to get rid of headers & other
%unwanted data from a worksheet


for i = 1:length(BB),
    [dd,ia,ib]=intersect(b(:,1),BB(i,:));
      if ~isempty(ia),
    phi_gg(i) = a(ia,3);
    theta_gg(i) = a(ia,2);
          else,
          phi_gg(i) = NaN;
          theta_gg(i) = NaN;
          display(i);
      end;
end;

station_name = BB;


X = squeeze(DataXYZ(:,1,:));
Y = squeeze(DataXYZ(:,2,:));
Z = squeeze(DataXYZ(:,3,:));
readme = 'Extracted from SPIDR data base. 116 stations July 2000. Converted some stations from HD to XY component \n Lat Long info of two stations VIZ & PBK are missing best -manoj March 23, 2009'

save c:\manoj\projects\alexei_gic_jul2000\July2000Minute X Y Z  station_name phi_gg theta_gg readme fday;

